package dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class Demo3 {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
    @Test
    public void Account() throws SQLException {
        //从连接池中获取连接
        Connection con = null;
        try {
            con = dataSource.getConnection();
            //创建有事务的可执行对象
            QueryRunner qr = new QueryRunner();
            //开启事务
            con.setAutoCommit(false);
            String sql = "update account set money=money-? where name =?";

            int row = qr.update(con, sql, 300, "lxl");
            if (row > 0) {
                System.out.println("转出成功");
            } else {
                System.out.println("转出失败");
            }

            sql = "update account set money =money+? where name =?";
            //pgone转出成功 李小璐收不到前
//            System.out.println(1/0);
            int row1 = qr.update(con, sql, 300, "pgone");
            if (row1>0) {
                System.out.println("转入成功");
            }else{
                System.out.println("转入失败");
            }
            //提交事务
            con.commit();
        } catch (Exception e) {
            con.rollback();
            e.printStackTrace();
        }finally {
            //手动归还
            con.close();
        }


    }
}
